package com.itheima.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.mapper.NursingProjectMapper;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.nursing.NursingProject;
import com.itheima.pojo.nursing.NursingProjectParam;
import com.itheima.pojo.vo.NursingProjectVo;
import com.itheima.service.NursingProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    //@Override
    //public PageResult page(NursingProjectParam nursingProjectParam) {
    //    PageHelper.startPage(nursingProjectParam.getPage(),nursingProjectParam.getPageSize());
    //    List<NursingProject> page = nursingProjectMapper.getPage(nursingProjectParam);
    //    Page<NursingProject> list = (Page<NursingProject>) page;
    //    return new PageResult(list.getTotal(),list.getResult());
    //}

    //@Override
    //public PageResult<NursingProject> page(NursingProjectParam param) {
    //    // 1. 创建 MyBatis-Plus 分页对象
    //    com.baomidou.mybatisplus.extension.plugins.pagination.Page<NursingProject> page
    //            = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(param.getPage(), param.getPageSize());
    //
    //    // 2. 执行分页查询（需确保 Mapper 中方法支持分页）
    //    nursingProjectMapper.selectPage(page, null);
    //
    //    // 3. 返回分页结果
    //    return PageResult.<NursingProject>builder()
    //            .total(page.getTotal())
    //            .rows(page.getRecords())
    //            .build();
    //}
    @Override
    public PageResult<NursingProject> page(NursingProjectParam param) {
        // 1. 创建分页对象
        Page<NursingProject> page = new Page<>(param.getPage(), param.getPageSize());

        // 2. 构建查询条件（根据 name 模糊查询）
        LambdaQueryWrapper<NursingProject> queryWrapper = new LambdaQueryWrapper<>();
        if (param.getName() != null && !param.getName().isEmpty()) {
            queryWrapper.like(NursingProject::getName, param.getName());
        }

        // 3. 执行分页查询（传入查询条件）
        nursingProjectMapper.selectPage(page, queryWrapper);

        // 4. 返回分页结果
        return PageResult.<NursingProject>builder()
                .total(page.getTotal())
                .rows(page.getRecords())
                .build();
    }

}